<?php

function uploadFile()
{
    $fileUploaded = false;
    if(isset($_FILES['my_file']))
    {
        $uploadedFileInfo = $_FILES['my_file'];
        $tmpFilePath = $uploadedFileInfo['tmp_name'];
        $realFileName = $uploadedFileInfo['name'];
        $realFileName = getUniqueFilename($realFileName);
        $saveFilePath = SITE_ROOT . "\\uploaded\\" . $realFileName;
        $fileUploaded = move_uploaded_file($tmpFilePath, $saveFilePath);
        $fileUploaded = move_uploaded_file($tmpFilePath, $saveFilePath);
    }
    echo fileUploadErrorMessage($_FILES['my_file']['error']);

    return $fileUploaded;
}

function fileUploadErrorMessage($error)
{
    switch ($error) 
    { 
        case UPLOAD_ERR_OK:
            return 'The file uploaded with success';
        case UPLOAD_ERR_INI_SIZE: 
            return 'The uploaded file exceeds the upload_max_filesize directive in php.ini'; 
        case UPLOAD_ERR_FORM_SIZE: 
            return 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form'; 
        case UPLOAD_ERR_PARTIAL: 
            return 'The uploaded file was only partially uploaded'; 
        case UPLOAD_ERR_NO_FILE: 
            return 'No file was uploaded'; 
        case UPLOAD_ERR_NO_TMP_DIR: 
            return 'Missing a temporary folder'; 
        case UPLOAD_ERR_CANT_WRITE: 
            return 'Failed to write file to disk'; 
        case UPLOAD_ERR_EXTENSION: 
            return 'File upload stopped by extension'; 
        default: 
            return 'Unknown upload error'; 
    } 
} 

function getFileExtension($fullFileName)
{
    return substr($fullFileName, strrpos($fullFileName, '.'));
}

function getPiecesCount($pieces)
{
    $countPieces = 0;
    while(isset($pieces[$countPieces]))
    {
        $countPieces++;
    }
    return ($countPieces - 2);     
}

function getCurrCopyIndex($pieces, $countPieces)
{
    return intval(substr($pieces[$countPieces], strrpos($pieces[$countPieces], '(') + 1));
}

function getFileName($pieces, $countPieces)
{
    $fileName = "";
    $currPiece = 0;
    while($currPiece <= $countPieces)
    {
        if($currPiece == 0)
        {
            $fileName = $fileName . $pieces[$currPiece];
        }
        else
        {
            $fileName = $fileName . "." . $pieces[$currPiece];
        }
        $currPiece++;
    }    
    return $fileName;
}

function getFileNameWithIndex($currCopyIndex, $isFirstCopy, $fullFileName, $fileName, $extension)
{
    if ( $isFirstCopy )
    {
        return ($fileName . "(1)" . $extension);
    }
    else
    {
        $search = "(" . $currCopyIndex . ")";
        $replace = "(" . ($currCopyIndex + 1) . ")";
        return str_replace( $search, $replace, $fullFileName );
    }
}

function getNewFileName($currCopyIndex, $fullFileName, $fileName, $extension)
{
    if ( isValidCurrCopyIndex($currCopyIndex) )
    {
        $fullFileName = getFileNameWithIndex($currCopyIndex, false, $fullFileName, $fileName, $extension);
        if ($fullFileName == $fileName . $extension)
        {       
             return getFileNameWithIndex($currCopyIndex, true, $fullFileName, $fileName, $extension);
        }
        else
        {
            return $fullFileName;
        }
    }
    else
    {
        return $fullFileName;
    }
}

function isValidCurrCopyIndex($currCopyIndex)
{
    return $currCopyIndex < MAX_COPY_INDEX;
}

function getUniqueFilename($fullFileName)
{
    $file = UPLOAD_DIR . $fullFileName;

    if ( file_exists($file) ) 
    {
        while ( file_exists($file) && isValidCurrCopyIndex($currCopyIndex) )
        {
            $extension = getFileExtension($fullFileName);
            $pieces = explode(".", $fullFileName);
            $countPieces = getPiecesCount($pieces);
	    $fileName = getFileName($pieces, $countPieces);
            $currCopyIndex = getCurrCopyIndex($pieces, $countPieces);
            $fullFileName = getNewFileName($currCopyIndex, $fullFileName, $fileName, $extension);
            $file = UPLOAD_DIR . $fullFileName;
        }
        return  $fullFileName;
    }              
    return $fullFileName;
}

?>